﻿create procedure dbo.SaveMenuItem
(
		@MenusID int,
		@Description varchar(2000) = '',
		@Price money,
		@PrepTimeMinutes int = 0,
		@MenuItemID int = null OUTPUT
		
	)

AS
	DECLARE @iCount int
	
	if @MenuItemID = null
		BEGIN
			INSERT INTO dbo.MenuItems
			                      (MenusId, Description, Price, PrepTimeMinutes)
			VALUES     (@MenusID,@Description,@Price,@PrepTimeMinutes)
			
			SET @MenuItemID = SCOPE_IDENTITY()
		END
	ELSE
		BEGIN
			SET @iCount = (SELECT Count(Id) FROM dbo.MenuItems  WHERE ID = @MenuItemID)
			
			if @iCount > 0
				BEGIN
					UPDATE    dbo.MenuItems
					SET              Description = @Description, Price = @Price, PrepTimeMinutes = @PrepTimeMinutes
				END
			ELSE
				BEGIN
					INSERT INTO dbo.MenuItems
			                      (MenusId, Description, Price, PrepTimeMinutes)
					VALUES     (@MenusID,@Description,@Price,@PrepTimeMinutes)
					
					SET @MenuItemID = SCOPE_IDENTITY()
				END
			
		END
	
	RETURN


